<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/admin/list">管理员列表</a></el-breadcrumb-item>
    </el-breadcrumb>

    <h3 style="margin: 20px 0;">列表详情</h3>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="200" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="200" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号码" width="200" align="center"></el-table-column>
      <el-table-column prop="email" label="电子邮件" width="200" header-align="center"></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center"></el-table-column>
      <el-table-column label="是否启用" width="80" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="handleChangeEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import Common from "@/views/common/Common"

export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    openDeleteConfirm(id) {
      this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id);
      }).catch(() => {
      });
    },
    handleEdit(id) {
      // alert('你点击了id=' + id + '的管理员数据的【编辑】按钮！');
    },
    handleDelete(id) {
      // alert('你点击了id=' + id + '的管理员数据的【删除】按钮！');
      let url = Common.data.BASEURI+'/ams/admin/'+id+'/delete';
      this.axios
          .create({'headers': {'Authorization': "Bearer " + localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state != 200) {
          this.$message.error(responseBody.message);
        }
        this.loadAdminList();
      });
    },
    handleChangeEnable(admin) {
      // // alert('你点击了id=' + admin.id + '的管理员数据的【启用】开关！新的状态为：' + admin.enable);
      // let enableText = ['禁用', '启用'];
      // let url = 'http://localhost:9081/admins/' + admin.id;
      // if (admin.enable == 1) {
      //   url += '/enable';
      // } else {
      //   url += '/disable';
      // }
      // console.log('url = ' + url);
      // this.axios
      //     .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
      //     .post(url).then((response) => {
      //   let responseBody = response.data;
      //   if (responseBody.state == 20000) {
      //     let message = '将【' + admin.username + '】的账号状态改为【' + enableText[admin.enable] + '】成功';
      //     console.log(message);
      //     this.$message({
      //       message: message,
      //       type: 'success'
      //     });
      //   } else {
      //     this.$message.error(responseBody.message);
      //   }
      // });
    },
    loadAdminList() {
      console.log('准备从服务器获取管理员列表……');
      let url = Common.data.BASEURI+'/ams/admin/getList';
      this.axios
          .create({'headers': {'Authorization': "Bearer " + localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        console.log('服务器端响应的结果：')
        console.log(response);
        let responseBody = response.data;
        if (responseBody.state == 200) {
          this.tableData = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    }
  },
  created() {
    console.log('执行生命周期方法：created');
  },
  mounted() {
    console.log('执行生命周期方法：mounted');
    this.loadAdminList();
  }
}
</script>
